//
//  WFPoticViewController.m
//  WangYiFirst
//
//  Created by 蔡靖方 on 16/9/11.
//  Copyright © 2016年 111. All rights reserved.
//

#import "WFPoticViewController.h"
#import "WFWenBaViewController.h"
#import "WFHuaTiViewController.h"
#import "WFGuanZhuViewController.h"
@interface WFPoticViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UIView *whiteView;
@property(nonatomic,strong)UIScrollView *Sview;
@property(nonatomic,strong)NSMutableArray *arrM;
@property(nonatomic,strong)NSMutableArray *arrBtn;
@end

@implementation WFPoticViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrBtn = [NSMutableArray array];
    _arrM = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addItem];
    [self addWhiteView];
    [self addTitle];
    [self addChildView];
    [self addScroll];
    [self selct];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollView.delegate = self;
}
- (void)selct
{
    [self click:_arrBtn[0]];
}
//添加navigation按钮
- (void)addItem
{
    //左侧按钮---------------------
    UIButton *liftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [liftBtn setImage:[UIImage imageNamed:@"putong"] forState:UIControlStateNormal];
    [liftBtn setImage:[UIImage imageNamed:@"xuanzhong"] forState:UIControlStateHighlighted];
    liftBtn.frame = CGRectMake(0, 0, 20, 20);
    //[liftBtn sizeToFit];
    [liftBtn addTarget:self action:@selector(clickLift) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *lift = [[UIBarButtonItem alloc] initWithCustomView:liftBtn];
    self.navigationItem.leftBarButtonItem = lift;
    //右侧按钮-----------------------
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 20, 20);
    [rightBtn setImage:[UIImage imageNamed:@"xiaoren"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"xiaoren"] forState:UIControlStateHighlighted];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = right;
}
- (void)clickLift
{
    NSLog(@"lift");
}
- (void)clickRight
{
    NSLog(@"right");
}
#pragma mark 添加title
- (void)addTitle
{
   
    NSArray *arr = @[@"问吧",@"话题",@"关注"];
    for(int i=0;i<arr.count;i++)
    {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10+i*(30+20), 0, 30, 30)];
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [_arrBtn addObject:btn];
        [_Sview addSubview:btn];
    }
    
}
- (void)click:(UIButton *)sender
{
    self.btn.selected = NO;
    sender.selected = YES;
    self.btn = sender;
        for (UIView *view in _arrM)
        {
            if(view.tag == sender.tag)
            {
                view.hidden = NO;
            }
            else
            {
                view.hidden = YES;
            }
        }
     self.scrollView.contentOffset = CGPointMake(sender.tag*self.scrollView.frame.size.width, 0);
}
#pragma mark 添加控制器
- (void)addChildView
{
    [self addChildViewController:[[WFWenBaViewController alloc] init]];
    [self addChildViewController:[[WFHuaTiViewController alloc] init]];
    [self addChildViewController:[[WFGuanZhuViewController alloc] init]];
}
#pragma mark scrollerView上添加控制器
- (void)addScroll
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    [self.view addSubview:_scrollView];
    NSInteger count = self.childViewControllers.count;
    for(int i=0;i<count;i++)
    {
        self.childViewControllers[i].view.frame = CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        [_scrollView addSubview:self.childViewControllers[i].view];
    }
    _scrollView.contentSize = CGSizeMake(count*self.scrollView.frame.size.width, 0);
    _scrollView.pagingEnabled = YES;
}
#pragma mark 添加view
- (void)addWhiteView
{
    _Sview = [[UIScrollView alloc] init];
    _Sview.frame = CGRectMake(0, 0, 150, 30);
    UIImageView *img = [[UIImageView alloc] init];
    img.frame = _Sview.bounds;
    img.image = [UIImage imageNamed:@"concerned_border"];
    [_Sview addSubview:img];
    for(int i=0;i<3;i++)
    {
        _whiteView = [[UIView alloc] initWithFrame:CGRectMake(i*50, 0, 50, self.Sview.frame.size.height)];
        _whiteView.tag = i;
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.hidden = YES;
        _whiteView.layer.cornerRadius = 15;
        _whiteView.clipsToBounds = YES;
        [_arrM addObject:_whiteView];
        [_Sview addSubview:_whiteView];
    }
   
    self.navigationItem.titleView = _Sview;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int lenght = scrollView.contentOffset.x;
    NSInteger count = lenght/scrollView.frame.size.width;
    [self click:_arrBtn[count]];
}
@end
